﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace NRemedy.CodeGenerator.WinForm
{
    public class CodeGenerateThread : ARThread
    {
        private string input = string.Empty;

        public PushCodeDelegate GenerateCompeleted;

        public CodeGenerateThread()
        {
            _server = ARServerFactory.CreateARServer();
            GenerateCompeleted += new PushCodeDelegate(CodeGenereteThread_GenerateCompeleted);
        }

        protected virtual void CodeGenereteThread_GenerateCompeleted(string code)
        {
 
        }

        public void Start(params object[] param)
        {
            _thread = new Thread(GenerateCode);
            input = param[0].ToString();
            _thread.Start();
        }


        private void GenerateCode()
        {
            CSharpClassGenerator generator = new CSharpClassGenerator(_context);

            string code = generator.GenerateClassByFormName(input);

            if (GenerateCompeleted != null)
            {
                Control control = GenerateCompeleted.Target as Control;
                if (control != null)
                    control.Invoke(GenerateCompeleted, code);
                else
                    GenerateCompeleted(code);
            }

            _server.LogOut();
 
        }
    }
}
